#include "videoWidget.h"

videoWidget::videoWidget(QWidget *parent) : QWidget(parent)
{
    m_layout = new QHBoxLayout(this);
    m_videoLabel= new QLabel();
    m_layout->addWidget(m_videoLabel);
    m_layout->setMargin(0);
    m_qcamera = new CVCamera();
    m_videoLabel->setScaledContents(true);

    connect(m_qcamera, &CVCamera::senderPixmap, this, [=](const QPixmap& currentPixmap){
        // m_videoLabel->setPixmap(currentPixmap.scaled(m_videoLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        m_videoLabel->setPixmap(currentPixmap);
        // qDebug() << "当前窗口Label大小为: " << m_videoLabel->size() << "图片大小为: " << currentPixmap.size();
    });
    m_qcamera->start();
}

bool videoWidget::slotChangeCamera(const QString& indexText)
{
    int index = -1;
    QList<QCameraInfo> camers = QCameraInfo::availableCameras();
    for(int i = 0; i < camers.size(); i++)
    {
        if(indexText == camers[i].description())                            // 找到对应该摄像头名称索引
        {
            index = i;
            break;
        }
    }

    if(index == -1)
    {
        ELOG("不存在当前摄像头!!");
        return false;
    }
    m_qcamera->openCamera(index);
    return true;
}

bool videoWidget::setCameraValue(signalKind kind, int value)
{
    switch(kind)
    {
    case signalKind::EXPOSURE:
        m_qcamera->setExposureNum(value);
        break;
    case signalKind::BUFFER:
        m_qcamera->setBufferNum(value);
        break;
    case signalKind::LUM:
        m_qcamera->setImgSetting(-1, value, -1, -1, -1);
        break;
    case signalKind::COMP:
        m_qcamera->setImgSetting(-1, -1, value, -1, -1);
        break;
    case signalKind::GAMMA:
        m_qcamera->setImgSetting(value, -1, -1, -1, -1);
        break;
    case signalKind::CHROMA:
        m_qcamera->setImgSetting(-1, -1, -1, value, -1);
        break;
    case signalKind::VIDEO:
        m_qcamera->setRecordVideo((btnStatus)value);
        break;
    case signalKind::IMAGE:
        m_qcamera->GetImage();
        break;
    case signalKind::Track:
        m_qcamera->OpenVideoTrack((bool)value);
        break;
    default:
        return false;
    }
//    if(value < 10)
//    {
//        ELOG("曝光时长过小，修改失败");
//        return false;
//    }

    return true;
}

void videoWidget::setBackGroundColor(const QColor &color)
{
    QPalette palette;
    palette.setBrush(QPalette::Background, color);
    this->setPalette(palette);
    this->setAutoFillBackground(true);
    // qDebug() << "设置背景色" << color;
}
